import 'package:equatable/equatable.dart';

/// 动漫实体类
/// 领域层的核心业务对象
class Anime extends Equatable {
  final int id;
  final String title;
  final String titleJapanese;
  final String? titleEnglish;
  final List<String> titleSynonyms;
  final String imageUrl;
  final String synopsis;
  final double score;
  final String status;
  final int episodes;
  final int? year;
  final String season;
  final List<String> genres;
  final List<String> studios;
  final String? trailer;
  final String? url;
  final int? ranked;
  final int? popularity;
  final int? members;
  final int? favorites;
  final String? aired;
  final String? rating;

  const Anime({
    required this.id,
    required this.title,
    required this.titleJapanese,
    this.titleEnglish,
    this.titleSynonyms = const [],
    required this.imageUrl,
    required this.synopsis,
    required this.score,
    required this.status,
    required this.episodes,
    this.year,
    required this.season,
    required this.genres,
    required this.studios,
    this.trailer,
    this.url,
    this.ranked,
    this.popularity,
    this.members,
    this.favorites,
    this.aired,
    this.rating,
  });

  @override
  List<Object?> get props => [
        id,
        title,
        titleJapanese,
        titleEnglish,
        titleSynonyms,
        imageUrl,
        synopsis,
        score,
        status,
        episodes,
        year,
        season,
        genres,
        studios,
        trailer,
        url,
        ranked,
        popularity,
        members,
        favorites,
        aired,
        rating,
      ];

  /// 复制并修改
  Anime copyWith({
    int? id,
    String? title,
    String? titleJapanese,
    String? titleEnglish,
    List<String>? titleSynonyms,
    String? imageUrl,
    String? synopsis,
    double? score,
    String? status,
    int? episodes,
    int? year,
    String? season,
    List<String>? genres,
    List<String>? studios,
    String? trailer,
    String? url,
    int? ranked,
    int? popularity,
    int? members,
    int? favorites,
    String? aired,
    String? rating,
  }) {
    return Anime(
      id: id ?? this.id,
      title: title ?? this.title,
      titleJapanese: titleJapanese ?? this.titleJapanese,
      titleEnglish: titleEnglish ?? this.titleEnglish,
      titleSynonyms: titleSynonyms ?? this.titleSynonyms,
      imageUrl: imageUrl ?? this.imageUrl,
      synopsis: synopsis ?? this.synopsis,
      score: score ?? this.score,
      status: status ?? this.status,
      episodes: episodes ?? this.episodes,
      year: year ?? this.year,
      season: season ?? this.season,
      genres: genres ?? this.genres,
      studios: studios ?? this.studios,
      trailer: trailer ?? this.trailer,
      url: url ?? this.url,
      ranked: ranked ?? this.ranked,
      popularity: popularity ?? this.popularity,
      members: members ?? this.members,
      favorites: favorites ?? this.favorites,
      aired: aired ?? this.aired,
      rating: rating ?? this.rating,
    );
  }

  /// 获取最佳显示标题（优先中文映射 > 日文 > 英文 > 原标题）
  String get bestTitle {
    // 1. 尝试中文映射
    final chineseTitle = _getChineseMapping();
    if (chineseTitle != null && chineseTitle != title) {
      return chineseTitle;
    }

    // 2. 如果有日文标题且不为空
    if (titleJapanese.isNotEmpty) {
      return titleJapanese;
    }

    // 3. 如果有英文标题
    if (titleEnglish != null && titleEnglish!.isNotEmpty) {
      return titleEnglish!;
    }

    // 4. 返回原标题
    return title;
  }

  /// 尝试获取中文映射
  String? _getChineseMapping() {
    // 这里可以集成ChineseTitleMapper的逻辑
    // 或者让调用方处理
    return null;
  }

  /// 格式化评分显示
  String get formattedScore {
    if (score == 0) return 'N/A';
    return score.toStringAsFixed(1);
  }

  /// 获取年份字符串
  String get yearString {
    if (year == null) return '未知';
    return '$year年';
  }

  /// 获取类型字符串
  String get genresString {
    if (genres.isEmpty) return '未分类';
    return genres.join(' · ');
  }

  /// 是否正在播出
  bool get isAiring {
    return status == '连载中' || status == 'Currently Airing';
  }

  /// 是否已完结
  bool get isFinished {
    return status == '已完结' || status == 'Finished Airing';
  }
}

/// 动漫类型
class Genre extends Equatable {
  final int id;
  final String name;

  const Genre({
    required this.id,
    required this.name,
  });

  @override
  List<Object> get props => [id, name];
}

/// 制作公司
class Studio extends Equatable {
  final int id;
  final String name;

  const Studio({
    required this.id,
    required this.name,
  });

  @override
  List<Object> get props => [id, name];
} 